package com.lframework.xingyun.basedata.impl.product;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lframework.starter.web.core.impl.BaseMpServiceImpl;
import com.lframework.starter.web.core.utils.OpLogUtil;
import java.io.Serializable;
import com.lframework.starter.web.core.utils.IdUtil;
import com.lframework.starter.web.core.annotations.oplog.OpLog;
import com.lframework.starter.web.inner.components.oplog.OtherOpLogType;
import com.lframework.xingyun.basedata.entity.ProductSpecCostDetail;
import com.lframework.xingyun.basedata.mappers.ProductSpecCostDetailMapper;
import com.lframework.xingyun.basedata.service.product.ProductSpecCostDetailService;
import com.lframework.xingyun.basedata.vo.product.speccost.ProductSpecCostDetailVo;
import com.lframework.xingyun.basedata.vo.product.speccost.QueryProductSpecCostDetailVo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductSpecCostDetailServiceImpl extends BaseMpServiceImpl<ProductSpecCostDetailMapper, ProductSpecCostDetail> implements ProductSpecCostDetailService {

    @Override
    public List<ProductSpecCostDetail> query(QueryProductSpecCostDetailVo vo) {

        return getBaseMapper().query(vo);
    }

    @Cacheable(value = ProductSpecCostDetail.CACHE_NAME, key = "@cacheVariables.tenantId() + #id", unless = "#result == null")
    @Override
    public ProductSpecCostDetail findById(String id) {

        return getBaseMapper().selectById(id);
    }

    @OpLog(type = OtherOpLogType.class, name = "新增商品规格成本明细，ID：{}", params = {"#id"})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public String save(String mainProductId, List<ProductSpecCostDetailVo> vo) {

        getThis(ProductSpecCostDetailService.class).remove(Wrappers.lambdaUpdate(ProductSpecCostDetail.class)
                .eq(ProductSpecCostDetail::getMainProductId, mainProductId));

        List<ProductSpecCostDetail> datas = new ArrayList<>();

        for (ProductSpecCostDetailVo productSpecCostDetailVo : vo) {
            ProductSpecCostDetail data = new ProductSpecCostDetail();
            data.setMainProductId(mainProductId);
            data.setPropertyId(productSpecCostDetailVo.getPropertyId());
            data.setItemId(productSpecCostDetailVo.getItemId());
            data.setSourceProductId(productSpecCostDetailVo.getSourceProductId());
            data.setProductId(productSpecCostDetailVo.getProductId());
            data.setUnitId(productSpecCostDetailVo.getUnitId());
            data.setConsumeQuantity(productSpecCostDetailVo.getConsumeQuantity());
            data.setType(productSpecCostDetailVo.getType());
            data.setUnitCost(productSpecCostDetailVo.getUnitCost());
            data.setTotalCost(productSpecCostDetailVo.getTotalCost());
            datas.add(data);

            for (ProductSpecCostDetailVo replacement : productSpecCostDetailVo.getReplacements()) {
                ProductSpecCostDetail replacementData = new ProductSpecCostDetail();
                replacementData.setMainProductId(mainProductId);
                replacementData.setPropertyId(replacement.getPropertyId());
                replacementData.setItemId(replacement.getItemId());
                replacementData.setSourceProductId(data.getProductId());
                replacementData.setProductId(replacement.getProductId());
                replacementData.setConsumeQuantity(replacement.getConsumeQuantity());
                replacementData.setType(replacement.getType());
                replacementData.setUnitCost(replacement.getUnitCost());
                replacementData.setTotalCost(replacement.getTotalCost());
                datas.add(replacementData);
            }
        }

        saveBatch(datas);

        OpLogUtil.setVariable("id", mainProductId);
        OpLogUtil.setExtra(vo);

        return mainProductId;
    }

    @CacheEvict(value = ProductSpecCostDetail.CACHE_NAME, key = "@cacheVariables.tenantId() + #key")
    @Override
    public void cleanCacheByKey(Serializable key) {

    }
}

